package com.sl.au.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Division;
import com.sl.au.entity.Grading;
import com.sl.au.entity.GridAreaInfo;
import com.sl.au.entity.GridUserDetails;
import com.sl.au.entity.Template;
import com.sl.au.entity.TemplateDetails;
import com.sl.au.entity.User;
import com.sl.au.service.DivisionService;
import com.sl.au.service.GradingService;
import com.sl.au.service.GridAreaInfoService;
import com.sl.au.service.GridUserDetailsService;
import com.sl.au.service.TemplateDetailsService;
import com.sl.au.service.TemplateService;
import com.sl.au.service.UserService;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping(value = "/ui/griduser") 
public class GridUserController extends BaseController {

	@Autowired
	TemplateService templateService;
	@Autowired
	GradingService gradingService;
	@Autowired
	TemplateDetailsService templateDetailsService;
	@Autowired
	UserService userService;
	@Autowired
	GridAreaInfoService gridAreaInfoService;
	@Autowired
	DivisionService divisionService; 
	@Autowired
	GridUserDetailsService gridUserDetailsService;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index() {
		ModelAndView modelAndView = this.createLayoutView("GridUser/griduserIndex");
		List<Grading> gradings = gradingService.getRepository().findGradingAll();
		modelAndView.addObject("gradings", gradings);
		return modelAndView;
	}
	
	/**
	 * 获取部件区域树信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="getAllNodes",method = RequestMethod.GET)
	public Map<String,Object> getAllNodes(HttpServletRequest request)
	{
		Map<String,Object> map = new HashMap<String,Object>();
		List<Division> node = null;
		
		node = divisionService.getAllNodes();
		List<Map<String,Object>> nodeMap=new ArrayList<Map<String,Object>>();
		nodeMap=divisionService.getNodeMap(node);
		
		map.put("code", 1);
		map.put("nodes", nodeMap);
	
		return map;
	}
	
	/**
	 * 获取绑定区域网格的信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getGridAreaPage", method = RequestMethod.POST)
	public Map<String,Object> getGridAreaPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String divisionId=request.getParameter("divisionId");
		String key=request.getParameter("key");
		List<GridAreaInfo> modules=null;
		int count = 0;
		
		modules = gridAreaInfoService.getPageByDivisionAndKey(start, length, divisionId, "网格", key, null, null);
		count = gridAreaInfoService.getCountByDivisionAndKey(divisionId, "网格",key);
		
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 模板页面数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getpage", method = RequestMethod.POST)
	public Map<String,Object> getPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		List<Template> modules=null;
		long count = 0;
		
		modules = templateService.getPageByKey(start, length, key, null, null);
		count = templateService.getPageByKeyCount(key);
		
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	/**
	 * 模板页面数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getNotSetUser", method = RequestMethod.POST)
	public Map<String,Object> getNotSetUserPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String gridId=request.getParameter("gridId");
		List<User> modules = new ArrayList<User>();
		int count = 0;
		if(StringUtils.hasText(gridId)) {
			modules = gridUserDetailsService.getNotSetUserPageByKey(start, length, gridId, key, null, null);
			count = gridUserDetailsService.getNotSetUserPageByKeyCount(gridId, key);
		}
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}

	/**
	 * 模板页面数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "getSetUser", method = RequestMethod.POST)
	public Map<String,Object> getSetUserPage(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String gridId=request.getParameter("gridId");
		String gradingId=request.getParameter("gradingId");
		List<User> modules= new ArrayList<User>();
		int count = 0;
		if(StringUtils.hasText(gradingId) && StringUtils.hasText(gridId)) {
			modules = gridUserDetailsService.getSetUserPageByKey(start, length, gridId, gradingId, key, null, null);
			count = gridUserDetailsService.getSetUserPageByKeyCount(gridId, gradingId, key);
		}
		map.put("data", modules);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "getGrading/{gridId}", method = RequestMethod.POST)
	public Map<String,Object> getGrading(@PathVariable("gridId") String gridId) {
		Map<String,Object> map = new HashMap<String,Object>();
		List<Grading> gradings = gridUserDetailsService.getRepository().getGradingByGrid(gridId);
		map.put("gradings", gradings);
		return map;
	}
	
	@RequestMapping("/addGrading/{gridId}/{gradingId}")
	public MsgHelper addGrading(@PathVariable("gridId") String gridId, @PathVariable("gradingId") String gradingId) {
		MsgHelper msgHelper = MsgHelper.success();
		GridAreaInfo grid = gridAreaInfoService.getRepository().findOne(gridId);
		Grading grading = gradingService.getRepository().findOne(gradingId);
		GridUserDetails gridUserDetails = new GridUserDetails();
		gridUserDetails.setGrid(grid);
		gridUserDetails.setGrading(grading);
		gridUserDetailsService.getRepository().save(gridUserDetails);
		return msgHelper;
	}

	@RequestMapping("/deleteGrading/{gridId}/{gradingId}")
	public MsgHelper deleteGrading(@PathVariable("gridId") String gridId, @PathVariable("gradingId") String gradingId) {
		MsgHelper msgHelper = MsgHelper.success();
		gridUserDetailsService.getRepository().deleteByGridAndGrading(gridId, gradingId);
		return msgHelper;
	}

	@RequestMapping("/addUser/{gridId}/{gradingId}/{userId}")
	public MsgHelper addUser(@PathVariable("gridId") String gridId, @PathVariable("gradingId") String gradingId,  @PathVariable("userId") String userId) {
		MsgHelper msgHelper = MsgHelper.success();
		
		GridAreaInfo grid = gridAreaInfoService.getRepository().findOne(gridId);
		Grading grading = gradingService.getRepository().findOne(gradingId);
		User user = userService.getRepository().findOne(userId);
		GridUserDetails gridUserDetails = new GridUserDetails();
		gridUserDetails.setGrid(grid);
		gridUserDetails.setGrading(grading);
		gridUserDetails.setUser(user);
		gridUserDetailsService.getRepository().save(gridUserDetails);
		
		msgHelper.setMsg("人员设置成功");
		return msgHelper;
	}

	@RequestMapping("/removeUser/{gridId}/{gradingId}/{userId}")
	public MsgHelper removeUser(@PathVariable("gridId") String gridId, @PathVariable("gradingId") String gradingId,  @PathVariable("userId") String userId) {
		MsgHelper msgHelper = MsgHelper.success();
		gridUserDetailsService.getRepository().deleteGridUserDetailsByUser(gridId, gradingId, userId);
		msgHelper.setMsg("人员移除成功");
		return msgHelper;
	}
	
	@RequestMapping(value="/addUserAll/{gridId}/{gradingId}",method = RequestMethod.POST)
	public Map<String,Object> addUserAll(@PathVariable("gridId") String gridId, @PathVariable("gradingId") String gradingId, @RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		ArrayList<String> dataInfo = (ArrayList<String>) map.get("dataInfo");
		GridAreaInfo grid = gridAreaInfoService.getRepository().findOne(gridId);
		Grading grading = gradingService.getRepository().findOne(gradingId);
		for(String userId : dataInfo) {
			User user = userService.getRepository().findOne(userId);
			GridUserDetails gridUserDetails = new GridUserDetails();
			gridUserDetails.setGrid(grid);
			gridUserDetails.setGrading(grading);
			gridUserDetails.setUser(user);
			gridUserDetailsService.getRepository().save(gridUserDetails);
		}
		msg.put("msg", "人员设置成功!");
		return msg;
	};
	
	@RequestMapping(value="/removeUserAll/{gridId}/{gradingId}",method = RequestMethod.POST)
	public Map<String,Object> removeUserAll(@PathVariable("gridId") String gridId, @PathVariable("gradingId") String gradingId, @RequestBody Map<String,Object> map){
		Map<String,Object> msg = new HashMap<String,Object>();
		ArrayList<String> dataInfo = (ArrayList<String>) map.get("dataInfo");
		String[] users = new String[dataInfo.size()];
		dataInfo.toArray(users);
		gridUserDetailsService.getRepository().deleteAllGridUserDetailsByUsers(gridId, gradingId, users);
		msg.put("msg", "人员移除成功!");
		return msg;
	};
	
	@RequestMapping(value="/setTemplate/{gridId}/{templateId}",method = RequestMethod.POST)
	public Map<String,Object> setTemplate(@PathVariable("gridId") String gridId, @PathVariable("templateId") String templateId){
		Map<String,Object> msg = new HashMap<String,Object>();
		GridAreaInfo grid = gridAreaInfoService.getRepository().findOne(gridId);
		//清空原网格数据
		gridUserDetailsService.getRepository().deleteByGrid(gridId);
		//填充数据
		List<TemplateDetails> templateDetailsList = templateDetailsService.getRepository().getByTemplate(templateId);
		for(TemplateDetails templateDetails : templateDetailsList) {
			GridUserDetails gridUserDetails = new GridUserDetails();
			gridUserDetails.setGrid(grid);
			gridUserDetails.setGrading(templateDetails.getGrading());
			gridUserDetails.setUser(templateDetails.getUser());
			gridUserDetailsService.getRepository().save(gridUserDetails);
		}
		msg.put("msg", "模板数据填充成功!");
		return msg;
	};
}
